import {
    wxfileTobase64
} from '../../../utils/util.js'
import dayjs from 'dayjs'
// import {
//     insuranceRecharge
// } from '../../api/insurance.js'
import {
    getInsuranceProgramme,
    getTitleQuerylistApi,
    recharge
} from '../../../api/app'
import {
    addRechargeRecord
} from '../../api/contract'
import user from '../../../utils/user.js'
const app = getApp()
let base64 = ''
Page({

    /**
     * 页面的初始数据
     */
    data: {
        date: '',
        preview: '',
        money: '',
        bankTitle: '',
        bankNum: '',
        bankName: '',
        insureCompanyType: '',
        rechargeType: '',
        yearList: ['1年', '2年', '3年'],
        yearIndex: '0',
        bankLogo: '',
        isup: false,
        titleList: [],
        titleId: '',
        payerFlag: '',
        payerName: '',
        cindex: 0,
        isall: false,
        isresub: '',
        payMode: 1
    },
    onChangeSure() {
        this.data.titleList.forEach((ele, i) => {
            if (i === this.data.cindex) {
                this.setData({
                    titleId: ele.id,
                    payerFlag: ele.payerFlag,
                    payerName: ele.payerName,
                    payMode: ele.payerType===0 || ele.payerType===2 ? 1 : 3,
                    isup: false
                })
            }
        })
    },
    onChangeTitle(e) {
        this.data.titleList.forEach((ele, i) => {
            if (i === e.currentTarget.dataset.i) {
                ele.isselect = true
            } else {
                ele.isselect = false
            }
        })
        this.setData({
            cindex: e.currentTarget.dataset.i,
            titleList: this.data.titleList
        })
    },
    async getTitleQuerylist() {
        let res = await getTitleQuerylistApi({
            pageSize: this.data.isall ? 999 : 4,
            pageNum: 1
        })
        if (this.data.isresub!=='1') {
            res.list.forEach((ele) => {
                if (ele.isDefault) {
                    ele.isselect = true
                    this.setData({
                        titleId: ele.id,
                        payerFlag: ele.payerFlag,
                        payerName: ele.payerName,
                        payMode: ele.payerType===0 || ele.payerType===2 ? 1 : 3
                    })
                } else {
                    ele.isselect = false
                }
            })
        }
        this.setData({
            titleList: res.list
        })
    },
    onOpenPopup() {
        this.setData({
            isup: true
        })
    },
    closepopup() {
        this.setData({
            isup: false
        })
    },
    bindDateChange: function (e) {
        this.setData({
            date: e.detail.value
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        if (options.isresub === '1') {
            this.setData({
                isresub: '1',
                date: options.date?options.date:dayjs().format('YYYY-MM-DD'),
                payerName: options.titlename==='null'?'':options.titlename,
                payerFlag: options.titleflag==='null'?'':options.titleflag,
                money: options.price,
                titleId: options.titleid==='null'?'':options.titleid,
                payMode: options.paymode,
            })
        } else {
            this.setData({
                date: dayjs().format('YYYY-MM-DD'),
            })
        }
        this.setData({
            insureCompanyType: user.insureCompanyType,
            rechargeType: options.type
        })
        this.setData({
            bankTitle: user.openBank,
            bankNum: user.bankNo,
            bankName: user.bankName,
            bankLogo: user.bankLogo
        })
    },
    onLookAll() {
        wx.navigateTo({
            url: '/pages/company-title-list/company-title-list',
        })
    },
    onUnload() {
        base64 = ''
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {},
    onShow() {
        this.data.titleList = []
        // this.getTitleQuerylist()
        this.getInsuranceProgramme()
    },
    upload() {
        wx.chooseImage({
            count: 1,
            sizeType: ['compressed'],
            success: (res) => {
                wxfileTobase64(res.tempFilePaths[0]).then(base => {
                    base64 = base
                    this.setData({
                        preview: res.tempFilePaths[0]
                    })
                })
            }
        })
    },
    copy(event) {
        wx.setClipboardData({
            data: event.currentTarget.dataset.copy, // 这个是要复制的数据
        })
    },
    submit() {
        // if (!base64) {
        //     app.toast('请上传图片')
        //     return
        // }
        this.onAddRechargeRecord()

    },
    preview() {
        wx.previewImage({
            urls: [this.data.preview],
        })
    },
    // 切换年份
    onYearIndex(event) {
        const {
            index
        } = event.currentTarget.dataset
        this.setData({
            yearIndex: index
        })
    },
    // 根据参数获取内容
    async getInsuranceProgramme() {
        const result = await getInsuranceProgramme({
            dictType: 'ele_sign_plan'
        })
        result.forEach((item, index) => {
            result[index].singleAmount = (item.dictValue / item.dictLabel).toFixed(1)
        })
        this.setData({
            yearList: result
        })
    },
    OnToAddTitle() {
        wx.navigateTo({
            url: '/pages/add-company-title/add-company-title?type=add',
        })
    },
    async onAddRechargeRecord() {
        // const {
        //     dictLabel,
        //     dictValue
        // } = this.data.yearList[this.data.yearIndex]
        // addRechargeRecord({
        //     payMoney: dictValue,
        //     rechargeAmount: dictLabel,
        //     // voucherUrl: base64,
        //     receiptId: user.receiptId,
        //     payDate: this.data.date,
        //     payerName: this.data.payerName,
        //     payerFlag: this.data.payerFlag,
        //     titleId: this.data.titleId,
        //     payMode: this.data.payMode
        // }).then(async () => {
        //     await wx.navigateBack()
        //     app.toast('提交成功')
        // })
        recharge({
            useWay: 'EleRecharge',
            mchid: '1673440814',
            description: '账户充值',
            amount: {
                total: this.data.yearList[this.data.yearIndex].dictValue
            },
            attach: JSON.stringify({chgAmt: this.data.yearList[this.data.yearIndex].dictValue}),
            payer: {
                openid: user.openid
            }
        }).then(res=>{
            app.toast('续费成功')
            res.package = res.packageValue
            wx.requestPayment(res).then(()=>{
                wx.navigateTo({
                    url: '/pages/success/success?title=充值成功&content=您可以稍后查看您的账户余额&opinion=0&back=2'
                })
            })
        })
    }
})